package cn.stu.common.utils;


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Pattern;

public class MyTools {
    public static boolean typeTest(String str, String type) {
        boolean flag;
        String telNum = "(^((13[0-9])|(14[5-8])|(15([0-3]|[5-9]))|(16[6])|(17[0|4|6|7|8])|(18[0-9])|(19[8-9]))\\d{8}$)|(^((170[0|5])|(174[0|1]))\\d{7}$)|(^(14[1|4])\\d{10}$)";
        String emailAddress = "[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9]{2,4}";
        String faxNum = "^\\d{8,}$";
        String bankAccout = "([3-6]{1})([0-9]{15})([0-9]{3})";
        switch (type) {
            case "tel":
                flag = Pattern.matches(telNum, str);
                break;
            case "email":
                flag = Pattern.matches(emailAddress, str);
                break;
            case "fax":
                flag = Pattern.matches(faxNum, str);
                break;
            case "bank":
                flag = Pattern.matches(bankAccout, str);
                break;
            default:
                flag = false;
        }
        return flag;
    }

    public Integer turnSex(String sex) {
        if (sex.equals("男") || sex.equals("nan") || sex.equals("1")) {
            return 1;
        } else if (sex.equals("女") || sex.equals("nv") || sex.equals("0")) {
            return 0;
        }
        return null;
    }

    public static Date turnDate(String time) {
        if (time == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }


    public static void testMoveFile(String from, String to) throws IOException {
        //文件
        Path fromFile = Paths.get(from);
        //目标文件夹
        Path anotherDir = Paths.get(to);

        Files.createDirectories(anotherDir);

        Files.move(fromFile, anotherDir.resolve(fromFile.getFileName()),
                StandardCopyOption.REPLACE_EXISTING);

    }


    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";

    public static String generatePassword(int length) {
        StringBuilder password = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHARACTERS.length());
            password.append(CHARACTERS.charAt(index));
        }

        return password.toString();
    }

}
